//    Copyright (C) Kherty.  All rights reserved.
using System;

namespace OpenLS.Spreadsheet.StandardAddIn.Statistics
{
    internal class ExponentialDistribution : ContinuousDistribution
    {
        private double _mean;

        public ExponentialDistribution(double mean)
        {
            Mean = mean;
        }

        public double Mean
        {
            get { return _mean; }
            set
            {
                if (value <= 0.0)
                    throw new ArgumentOutOfRangeException("value");
                _mean = value;
            }
        }

        public override double CumulativeProbability(double x)
        {
            if (x <= 0.0)
                return 0.0;
            return 1.0 - Math.Exp(-x*Mean);
        }

        public double Probability(double x)
        {
            if (x < 0)
                throw new ArgumentOutOfRangeException("x");
            return Mean*Math.Exp(-Mean*x);
        }

        public override double InverseCumulativeProbability(double probability)
        {
            CheckValidProperty(probability);
            if (probability == 1.0)
                return double.PositiveInfinity;
            return -Mean*Math.Log(1 - probability);
        }

        protected override SolverParameters GetSolverParameters(double probability)
        {
            return new SolverParameters(0,
                                        probability < 0.5 ? Mean : double.MaxValue,
                                        probability < 0.5 ? Mean/2 : Mean);
        }
    }
}